package cn.gaotu.org.controller;

import cn.gaotu.basic.anno.PreAuthorize;
import cn.gaotu.basic.util.AjaxResult;
import cn.gaotu.basic.util.PageList;
import cn.gaotu.org.domain.Department;
import cn.gaotu.org.query.DepartmentQuery;
import cn.gaotu.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZouX
 * @date 2022/8/14 10:36
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门的api",description = "部门相关的CRUD")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    // http://localhost:8080/department/1 Get
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id")Long id){
        return departmentService.findById(id);
    }

    // http://localhost:8080/department Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    // http://localhost:8080/department/1 Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    @PreAuthorize(name="删除对应数据",value = "dept:remove ")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            departmentService.DeleteDep(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
    // http://localhost:8080/department Put 参数：{} - 请求体
    @PutMapping
    //@RequestBody Department department - 从请求体中获取数据赋值给Department对象
    //这个是添加或者更新
    @ApiOperation(value = "更新或者保存")
    @PreAuthorize(name="更新或者保存部门数据",value = "dept:update ")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }
    // http://localhost:8080/ Podepartmentst 参数：{} - 请求体
    //  查询分页
    @PostMapping
    @ApiOperation(value = "查询页面")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }
    @ApiOperation(value = "查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
